<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

use App\Goods;
use App\Category;
use App\Lable;
//use App\Ext\OSS;
use OSS\OssClient;
use OSS\Core\OssException;

class GoodsController extends Controller
{
    //定义查询数组
    protected static $field = [
        ['id'=>1,'searchtype'=>'编号'],
        ['id'=>2,'searchtype'=>'权重'],
        ['id'=>3,'searchtype'=>'创建时间'],
        ['id'=>4,'searchtype'=>'更新时间'],
        ['id'=>5,'searchtype'=>'商品名称'],
        ['id'=>6,'searchtype'=>'商品分类'],
        ['id'=>7,'searchtype'=>'商品描述'],
        ['id'=>8,'searchtype'=>'商品价格'],
//        ['id'=>9,'searchtype'=>'商品标签'],
        ['id'=>10,'searchtype'=>'商品库存'],

    ];

    //加载商品列表页
    public function index() {
        $search = Input::get('searchtype');
        $param = Input::get('param');
        $obj = new Goods;
        if($search == 1){
            $obj = $obj->where('id','like',"%$param%");
        }elseif ($search == 2){
            $obj = $obj->where('rank','like',"%$param%");
        }elseif ($search == 3){
            $obj = $obj->where('create_time','like',"%$param%");
        }elseif ($search == 4){
            $obj = $obj->where('update_time','like',"%$param%");
        }elseif ($search == 5){
            $obj = $obj->where('goods_name','like',"%$param%");
        }elseif ($search == 6){
            $id = Category::where('name','like',"%$param%")->first();
            if(!$id) {
                return redirect()->back()->withInput()->withErrors('为搜索到该分类!');
            }
            $obj = $obj->where('category_id','=',$id->id);
        }elseif ($search == 7){
            $obj = $obj->where('goods_introduce','like',"%$param%");
        }elseif ($search == 8){
            $obj = $obj->where('goods_price','like',"%$param%");
        }elseif ($search == 9){
            $obj = $obj->where('lable','like',"%$param%");
        }elseif ($search == 10){
            $obj = $obj->where('goods_inventory','like',"%$param%");
        }
        $list = $obj->Paginate(5);//dd($list['data']);
        if($list == false && !empty($search)){
            return redirect()->back()->withInput()->withErrors('未搜索到结果！');
        }

        $searchtype = self::$field;
        return view('admin.goods.index',['list'=>$list,'searchtype'=>$searchtype]);
    }

    //添加商品页
    public function add() {
        $cate = Category::all()->toArray();
        $lable = Lable::all()->toArray();
        return view('admin.goods.addGoods',['cate'=>$cate,'lable'=>$lable]);
    }

    //添加商品
    public function goodsAdd() {
        $name = input::get('name');
        $cate = input::get('cate');
        $intro = input::get('intro');
        $price = input::get('price');
        $picture = input::file('picture');
        $invent = input::get('invent');
        $lable = input::get('lable');
        if(empty($name) || empty($intro) || empty($price) || empty($invent)) {
            return redirect()->back()->withInput()->withErrors('缺少参数!');
        }
        $repetition = Goods::where('goods_name',$name)->first();
        if($repetition) {
            return redirect()->back()->withInput()->withErrors('该商品已存在!');
        }
        if(empty($picture)) {
            return redirect()->back()->withInput()->withErrors('请选择上传图片!');
        }
        if($picture->isValid()) {
            $allowed_extensions = ["png", "jpg", "gif"];
            if($picture->getClientOriginalExtension() && !in_array($picture->getClientOriginalExtension(),$allowed_extensions)) {
                return ['error' => 'You may only upload png, jpg or gif.'];
            }
            $destinationPath = 'productImg/'.date('Y-m-d');  //public文件夹下新建storage/uploads文件夹
            $extension = $picture->getClientOriginalExtension();
            $pictureName = str_random(10) . '.' . $extension;
            $picture->move($destinationPath,$pictureName);
            $picturePath = asset($destinationPath.$pictureName);
        }
        $list = Goods::insert([
            'goods_name' => $name,
            'category_id' => $cate,
            'goods_introduce' => $intro,
            'goods_price' => $price,
            'goods_picture' => date('Y-m-d') . '/' . $pictureName,
            'goods_inventory' => $invent,
//            'lable' => $lable,
        ]);
        if($list) {
            return Redirect::to('admin/goodsList')->withErrors('添加成功!');
        }
        else {
            return redirect()->back()->withInput()->withErrors('添加失败!');
        }
//        return 123;
    }

    //修改商品页
    public function getGoods($id) {
        $list = Goods::where('id',$id)->first();
        $cate = Category::all()->toArray();
        $lable = Lable::all()->toArray();
        if($list) {
            return view('admin.goods.updateGoods',['list'=>$list,'cate'=>$cate,'lable'=>$lable]);
        }
        else {
            return redirect()->back()->withInput()->withErrors('加载失败!');
        }
    }

    //更新商品
    public function updateGoods() {
        $id = input::get('id');
        $data['goods_name'] = input::get('name');
        $data['category_id'] = input::get('cate');
        $data['goods_introduce'] = input::get('intro');
        $data['goods_price'] = input::get('price');
        $data['goods_inventory'] = input::get('invent');
        $data['lable'] = input::get('lable');
        $picture = input::file('picture');

        if(empty($data['goods_name']) || empty($data['goods_introduce']) || empty($data['goods_price']) || empty($data['goods_inventory']) ) {
            return redirect()->back()->withInput()->withErrors('缺少参数!');
        }
        if(!empty($picture)) {
            if($picture->isValid()) {
                $allowed_extensions = ['png','jpg','gif'];
                if($picture->getClientOriginalExtension() && !in_array($picture->getClientOriginalExtension(),$allowed_extensions)) {
                    return ['error' => 'You may only upload png, jpg or gif.'];
                }
                $destinationPath = 'productImg/'.date('Y-m-d');
                $extension = $picture->getClientOriginalExtension();
                $pictureName = str_random(10). '.' . $extension;
                $picture->move($destinationPath,$pictureName);
                $picturePath = asset($destinationPath.$pictureName);
                $data['goods_picture'] = date('Y-m-d'). '/' . $pictureName;
            }
            else {
                return redirect()->back()->withInput()->withErrors('文件上传失败!');
            }
        }
        $list = Goods::where('id',$id)->update($data);
        if($list) {
            return Redirect::to('admin/goodsList')->withErrors('更新成功!');
        }
        else {
            return redirect()->back()->withInput()->withErrors('更新失败!');
        }
    }

    //删除商品
    public function delGoods($id) {
        $list = Goods::where('id',$id)->delete();
        if($list) {
            return Redirect::to('admin/goodsList')->withErrors('删除成功!');
        }
        else {
            return redirect()->back()->withInput()->withErrors('删除失败!');
        }
    }

    //批量删除商品
    public function batchDel() {
        $id = input::get('id');
        $list = Goods::whereIn('id',$id)->delete();
        if($list) {
            return Redirect::to('admin/goodsList')->withErrors('删除成功!');
        }
        else {
            return redirect()->back()->withInput()->withErrors('删除失败!');
        }
    }
}
